var map1 = null;
var map2 = null;
var map3 = null;
var map4 = null;
var scalebar1 = null;
var scalebar2 = null;
var scalebar3 = null;
var scalebar4 = null;
var symbolUrl = "../img/alarm.gif";
var defaultEventIcon = "../img/defaultEventType.png";
var defaultTVIcon = "../img/defaultTV.png";
var defaultPartIcon = "../img/defaultPart.png";

var arcGisUrl = "http://39.107.125.44:6080";
var map;
var toolbar;
var partLayer;
var centerX = 106.638;// 120.206354450751;
var centerY = 26.684;// 30.0725834699399;
var currentGraphic;
var drawHandler;
var drawHandler2;
var drawHandler3;
var drawHandler4;
var dynamicTowPointFiveD;
var extendScal = 10;
var graphic;
var registerPointType = "registerPointType";
var reportPointType = "reportPointType";
var tvPointType = "tvPointType";
var flickerPointType = "flickerPointType";
var partPointType = "partPointType";
var gridNetType = "gridNetType";

var newTempId = "registerTempId";
var flickerPointID = "flickerPointID";
var secondLayer;

var vec;
var veca;
var vec2;
var veca2;
var vec3;
var veca3;
var vec4;
var veca4;
require([ "esri/map", "esri/Color", "esri/graphic", "esri/InfoTemplate",
		"esri/SpatialReference", "esri/renderers/HeatmapRenderer",
		// 控件
		"esri/dijit/OverviewMap", "esri/dijit/Scalebar",
		"esri/dijit/Measurement", "esri/units",
		// 图层
		"tdlib/TDTLayer", "tdlib/TDTAnnoLayer", "tdlib/TDT_ImgLayer",
		"tdlib/TDT_ImgAnnoLayer", "esri/layers/GraphicsLayer",
		"esri/layers/ArcGISDynamicMapServiceLayer",
		"esri/layers/ArcGISTiledMapServiceLayer", "esri/layers/FeatureLayer",
		// 图形要素
		"esri/geometry/Point", "esri/geometry/Polyline",
		"esri/geometry/Polygon", "esri/geometry/Circle",
		// 样式
		"esri/symbols/TextSymbol", "esri/symbols/SimpleMarkerSymbol",
		"esri/symbols/PictureMarkerSymbol", "esri/symbols/SimpleLineSymbol",
		"esri/symbols/SimpleFillSymbol", "esri/toolbars/draw" ], function(Map,
		Color, Graphic, InfoTemplate, SpatialReference, HeatmapRenderer,
		OverviewMap, Scalebar, Measurement, Units, TDTLayer, TDTAnnoLayer,
		TDT_ImgLayer, TDT_ImgAnnoLayer, GraphicsLayer,
		ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer, FeatureLayer,
		Point, Polyline, Polygon, Circle, TextSymbol, SimpleMarkerSymbol,
		PictureMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, Draw) {

	map1 = new Map("unArchievedEventReg_allmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar1 = new Scalebar({
		map : map1,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar1.hide();
	drawHandler = Draw;
	vec = new TDTLayer();// 切片线画图
	veca = new TDTAnnoLayer();// 切片标注
	var img = new TDT_ImgLayer();
	var imga = new TDT_ImgAnnoLayer();
	map1.addLayer(vec);
	map1.addLayer(veca);

	map2 = new Map("waitRegister_allmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar2 = new Scalebar({
		map : map2,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar2.hide();
	drawHandler2 = Draw;
	vec2 = new TDTLayer();// 切片线画图
	veca2 = new TDTAnnoLayer();// 切片标注
	var img2 = new TDT_ImgLayer();
	var imga2 = new TDT_ImgAnnoLayer();
	map2.addLayer(vec2);
	map2.addLayer(veca2);

	map3 = new Map("archievedEventReg_allmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar3 = new Scalebar({
		map : map3,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar3.hide();
	drawHandler3 = Draw;
	vec3 = new TDTLayer();// 切片线画图
	veca3 = new TDTAnnoLayer();// 切片标注
	var img3 = new TDT_ImgLayer();
	var imga3 = new TDT_ImgAnnoLayer();
	map3.addLayer(vec3);
	map3.addLayer(veca3);
	
	map4 = new Map("history_allmap", {
		slider : false,
		logo : false,
		showLabels : true,
		center : [ centerX, centerY ],
		zoom : 17,
		minZoom : 8
	});

	scalebar4 = new Scalebar({
		map : map1,
		scalebarUnit : "dual",
		attachTo : 'top-center'
	});

	scalebar4.hide();
	drawHandler4 = Draw;
	vec4 = new TDTLayer();// 切片线画图
	veca4 = new TDTAnnoLayer();// 切片标注
	var img4 = new TDT_ImgLayer();
	var imga4 = new TDT_ImgAnnoLayer();
	map4.addLayer(vec4);
	map4.addLayer(veca4);


	clearInterval(flashTime);
	// 单位数据刷新
	var flashTime = setInterval(function() {
		refreashFlickerPoint();
	}, 75);

});

function refreashFlickerPoint() {
	if (flickerPoint) {
		var symbol = flickerPoint.symbol;

		symbol.color.a -= 0.05;
		symbol.setSize(symbol.size + 8);

		if (symbol.color.a <= 0) {
			symbol.color.a = 1;
			symbol.setSize(1);
		}
		flickerPoint.setSymbol(symbol);
	}
}
var flickerPoint;

/*function createPoint(str, data, symbolurl) {
	
	var symbol = new esri.symbol.PictureMarkerSymbol(symbolurl, 20, 20);

	var myPoint = {
		"geometry" : {
			"type" : "point",
			"x" : data.longitude,
			"y" : data.latitude,
			"spatialReference" : {
				"wkid" : 4326
			}
		},
		"attributes" : data,
		"symbol" : symbol
	};
	var graphic = getcurrentGraphic(data.id, flickerPointType);
	

	graphic.symbol = symbol;
	graphic.attributes = data;
	if (str == "#unArchievedEventReg") {
		map1.graphics.add(graphic);
		map1.centerAndZoom(graphic.geometry, map1.getLevel());
	}
	if (str == "#waitRegister") {
		map2.graphics.add(graphic);
		map2.centerAndZoom(graphic.geometry, map2.getLevel());
	}
	if (str == "#archievedEventReg") {
		map3.graphics.add(graphic);
		map3.centerAndZoom(graphic.geometry, map3.getLevel());
	}

};*/

function createFlickerPoint(data, str) {

	var symbol = new esri.symbol.SimpleMarkerSymbol(
			esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, // 样式,STYLE_CIRCLE(●),STYLE_CROSS(+),STYLE_DIAMOND(◆),STYLE_SQUARE(■),STYLE_X(X)
			1, // 像素
			new esri.symbol.SimpleLineSymbol(
					esri.symbol.SimpleLineSymbol.STYLE_SOLID, // 样式,STYLE_DASH(破折号),STYLE_DASHDOT(点划线),STYLE_DASHDOTDOT,STYLE_DOT(点),STYLE_NULL,STYLE_SOLID(实线)
					new dojo.Color([ 255, 0, 0 ]), 0.1), // 颜色 1 // 像素 ),
			new dojo.Color([ 255, 0, 0, 1 ]) // 颜色和透明度
	);
	var myPoint = {
		"geometry" : {
			"type" : "point",
			"x" : data.longitude,
			"y" : data.latitude,
			"spatialReference" : {
				"wkid" : 4326
			}
		},
		"attributes" : data,
		"symbol" : symbol
	};
	var graphic = getcurrentGraphic(data.id, str);

	if (graphic == null) {
		graphic = new esri.Graphic(myPoint);
		graphic.symbol = symbol;
		graphic.attributes = data;

	} else {
		var geometry = graphic.geometry;
		geometry.x = data.longitude;
		geometry.y = data.latitude;

		graphic.attributes = data;
		graphic.setGeometry(geometry);
		graphic.setSymbol(symbol);
	}

	if (str == "#unArchievedEventReg") {
		map1.graphics.add(graphic);
	}
	if (str == "#waitRegister") {
		map2.graphics.add(graphic);
	}
	if (str == "#archievedEventReg") {
		map3.graphics.add(graphic);
	}
	if (str == "#history") {
		map4.graphics.add(graphic);
	}
	flickerPoint = graphic;

	return graphic;
};

function setCenter(str) {
	if (str == "#unArchievedEventReg") {
		map1.centerAndZoom(flickerPoint.geometry, map1.getLevel());
	}
	if (str == "#waitRegister") {
		map2.centerAndZoom(flickerPoint.geometry, map2.getLevel());
	}
	if (str == "#archievedEventReg") {
		map3.centerAndZoom(flickerPoint.geometry, map3.getLevel());
	}
	if (str == "#history") {
		map4.centerAndZoom(flickerPoint.geometry, map4.getLevel());
	}
}

function getcurrentGraphic(data, str) {

	var graphics;

	if (str == "#unArchievedEventReg") {
		graphics = map1.graphics.graphics;
	}
	if (str == "#waitRegister") {
		graphics = map2.graphics.graphics;
	}
	if (str == "#archievedEventReg") {
		graphics = map3.graphics.graphics;
	}
	if (str == "#history") {
		graphics = map4.graphics.graphics;
	}

	for (var i = 0; i < graphics.length; i++) {
		if (graphics[i]) {
			if (graphics[i].attributes) {
				if (graphics[i].attributes) {
					return graphics[i];
				}
			}
		}
	}
	return null;
};
$(document).on("click",".hrefClickR",function() {
	var str="#history";
	var id = $(this).attr("defineId");
	$.ajax({
		url : rootPath+ "/ui/archive/findReportByReportId/"+ id,
		dataType : 'json',
		type : 'get',
		success:function(result){
			if(result.code==1){
				var data=result.data.report;
				var content=data.reportContent;
				if(content==''||undefined==content||content==null){
					content='未上报内容';
				}
				$(str + "_registerEventContent").html(content);
				var reportMemo=data.reportMemo;
				if(reportMemo==''||undefined==reportMemo||reportMemo==null){
					reportMemo='未上报备注';
				}
				$(str + "_registerEventMemo").html(reportMemo);
				
				$(str + "_registerEventType").html(data.reportEventType.eventName);
				
				$(str + "_registerEventAccTime").html(moment(data.reportAccTime).format("YYYY-MM-DD HH:mm"));
				//$(str + "_registerEventCreateUser").html(data.reportUser.name);
				var where=data.reportAdress;
				if(where==null||''==where||undefined==where){
					where="上报时未获取到上报地点";
				}
				$(str + "_where").html(where);
				

				var purl = data.reportImgAdress;
				if (purl == undefined || purl == "") {
					purl = "img/nopicture.png";
				}
				purl = window.rootPath + "/" + purl;
				$(str + "_pic").attr('src',purl);
				
				createFlickerPoint(data, str);
				setTimeout(function() {
					setCenter(str);
				}, 500);

			}
		}
	});
	$(this).prop("href",   str+"_mb");
});
$(document).on("click",".hrefClick",function() {
	var id = $(this).attr("defineId");
	var str = $(this).attr("defindStr");
	$(str + "_eventId").val(id);
	var eventId = $(str + "_eventId").val();
	
	// 数据填充
	$.ajax({
				url : rootPath+ "/ui/archive/findEventRegByEvendId/"+ id,
				dataType : 'json',
				type : 'get',
				success : function(result) {
					if (result.code == 1) {

						var data = result.data.event;
						$(str + "_registerEventName").html(data.registerEventName);
						$(str + "_registerEventContent").html(data.registerEventContent);
						$(str + "_registerEventMemo").html(data.registerEventMemo);
						$(str + "_registerEventIdentify").html(data.registerEventIdentify);
						$(str + "_registerEventType").html(result.data.parentType+ "&nbsp;&nbsp;"+ data.registerEventType.eventName);
						$(str + "_registerEventEvaluateLevel").html(data.registerEventEvaluateLevel.gradingName);
						$(str + "_registerEventAccTime").html(moment(data.registerEventAccTime).format("YYYY-MM-DD HH:mm"));
						$(str + "_registerEventCreateUser").html(data.registerEventCreateUser.name);
						$(str + "_registerEventSeriousLevel").html(data.registerEventSeriousLevel.title);
						$(str + "_longitude").html(data.longitude);
						$(str + "_latitude").html(data.latitude);
						$(str + "_registerEventToNetGrid").html(data.registerEventToNetGrid.gridName);
						$(str + "_where").html(data.registerEventAdress);
						
						var purl = data.registerEventImgAdress;
						if (purl == undefined || purl == "") {
							purl = "img/nopicture.png";
						}
						purl = window.rootPath + "/" + purl;
						$(str + "_pic").attr('src',purl);
						
						// 地图
						var lon = data.longitude;
						var lat = data.latitude

						createFlickerPoint(data, str);
						setTimeout(function() {
							setCenter(str);
						}, 500);

						if (str == '#archievedEventReg') {// 已完结
							
							$.ajax({
										url : rootPath+ "/ui/archive/findArchiveByRegisterId/"+ eventId,
										dataType : 'json',
										type : 'get',
										success : function(
												result) {
											if (result.code == 1) {
												var d = result.data.archive;
												var p=d.archiveAddr;
												if(p==undefined||''==p||p==null){
													p="归档时未能成功获取地址信息";
												}else{
													p='<img width="4%" src="/wcsi/img/yourlocation.png"/>'+p;
												}
												var content=d.content;
												if(content==''||content==undefined||content==null){
													content="该事件无上报内容";
												}
												var memo=d.memo;
												if(memo==''||memo==undefined||memo==null){
													memo="该事件无上报备注";
												}
												$("#archievedEventReg_archiveCon").html(content);
												$("#archievedEventReg_archiveMemo").html(memo);
												$("#archievedWhere").html(p);
												
												var picAddr='';
												
												if(d.photourl==undefined||d.photourl==null||d.photourl==''){
													picAddr=rootPath+"/img/nopicture.png";
												}else{
													picAddr=rootPath+"/"+d.photourl;
												}
												console.log("picAddr:"+picAddr);
											
												$("#archievedEventReg_pic2").attr('src',picAddr);
											}
										}
									})

						}
					}
				}
			});
	$(this).prop("href", str + "_mb");
});